#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
void writer (int wfd)
{
    const char *str = "hello father";
    char buffer[128];
    int cnt = 0;
    pid_t pid = getpid();
    while(1)
    {
        snprintf(buffer,sizeof(buffer),"message:%s,pid:%d,count:%d\n",str,pid,cnt);
        write(wfd,buffer,strlen(buffer));
        cnt++;
        sleep(1);
    }
}
void reader(int rfd)
{
    char buffer[1024];
    while (1)
    {
     ssize_t n = read(rfd,buffer,sizeof(buffer)-1);
     (void)n;
     printf("message:%s",buffer);   /* code */
    }
    
}
int main()
{
    int pipdefd[2];
    int n = pipe(pipdefd);
    if(n<0)
    {
        return 1;
    }
    printf("pipefd[0]:%d,pipefd[1]:%d\n",pipdefd[0],pipdefd[1]);
    pid_t id = fork();
    if(id==0)
    {
        close(pipdefd[0]);
        writer(pipdefd[1]);
        exit(1);
    }
    close(pipdefd[1]);
    reader(pipdefd[0]);
    return 0;
}
